package com.medical.medical.controller.zxsController;

import com.alibaba.fastjson.JSONArray;
import com.medical.medical.entity.Hospital;
import com.medical.medical.service.HospitalService;
import com.medical.medical.util.Constants;
import com.medical.medical.util.PageSupport;
import com.medical.medical.util.ResultUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @AuthorDesc 古娜拉黑暗之神，乌乌拉拉，代码运行。
 * @Author zxs
 * @Date 2022/9/14 9:46
 * @Version 1.0.0
 * @Description 医院数据请求层
 */

@Controller
@Slf4j
public class HospitalController {

    @Resource
    private HospitalService hospitalService;

    @RequestMapping("/viewHospital")
     /**
      *@author WangLuDong
      *@Description 方法描述 根据医院名查询医院信息,并完成分页
      *@Date 2022-09-21 15:19:24
       * @param hName
      * @param currentPage
      * @param resultMap
      *@Return {@link String}
      */
    public String getAllHospital(@RequestParam(value = "hName",required = false) String hName,
                                 @RequestParam(required = false) String currentPage,
                                  Map<String,Object> resultMap){
        log.info("获取医院信息开始");
        int current = 0;
        if (currentPage != null){
            try {
                current = Integer.valueOf(currentPage);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                log.error("解析当前页时失败");
            }
        }
        //总数量
        int totalCount = hospitalService.getHospitalCount(hName);
        if (totalCount == 0){
            Map<String, Object> result = ResultUtil.successNull("查询不到医院信息");
            resultMap.putAll(result);
            resultMap.put("hName",hName);
            //返回指定的空白页面
            return "doctor/margin";
        }
        //总数量
        PageSupport pages = new PageSupport();
        //当前页数
        pages.setCurrentPageNo(current);
        //一页的容量
        pages.setPageSize(Constants.PAGE_SIZE);
        //设置总数
        pages.setTotalCount(totalCount);
        //获取总页数
        int totalPageCount = pages.getTotalPageCount();

        //控制首页和尾页
        if (current < 1){
            current = 1;
        }else if (current > totalPageCount){
            current = totalPageCount;
        }


        Map <String,Object> result = hospitalService.getHospitalByHName(hName,current);
        log.info("{}",result);
        resultMap.putAll(result);

        resultMap.put("totalPageCount", totalPageCount);
        resultMap.put("totalCount", totalCount);
        resultMap.put("current", current);
        resultMap.put("hName", hName);
        //返回指定的空白页面
        return "admin/hospitallist";

    }
    /**
     * @description 跳转到医院添加页面
     * @author GUOYN
     * @date 2022-09-24 22:26:17
     * @return {@link String}
     */
    @RequestMapping("tohospitaladd")
    public String toHospitalAdd(){
        return "admin/hospitaladd";
    }
    @RequestMapping("/addHospital")
     /**
      *@author WangLuDong
      *@Description 方法描述 添加医院信息
      *@Date 2022-09-21 15:19:59
       * @param hospital
      *@Return {@link Object}
      */
    public Object addHospital(Hospital hospital,Model model){
        log.info("添加医院信息开始..");
        boolean addFlag = hospitalService.addHospital(hospital);
        if (addFlag) {
            return "redirect:viewHospital";
        }
        if (hospital.getHStatus().equals('1')){
            model.addAttribute("result","添加医院信息失败");
            return "admin/hospitaladd";
        }else {
            model.addAttribute("result","添加医院信息失败");
            return "admin/hospitaladd";
        }
    }

    @RequestMapping("/updateHospital")
     /**
      *@author WangLuDong
      *@Description 方法描述 根据医院id进行修改医院信息
      *@Date 2022-09-21 15:39:05
       * @param hId
      *@Return {@link Object}
      */
    public Object updateHospitalByHId(Hospital hospital,Model model){
        log.info("医院信息：{}",hospital);
        log.info("修改医院信息开始..");
        boolean updateFlag = hospitalService.updateHospitalByHId(hospital);
        if (updateFlag) {
            return "redirect:viewHospital";
        }else {
            if (hospital.getHStatus().equals('1')){
                model.addAttribute("result","修改医院信息失败");
                return "admin/hospitaluqdate";
            }else {
                model.addAttribute("result","审核医院信息失败");
                return "admin/hospitalaudit";
            }
        }


    }

     /**
      *@author WangLuDong
      *@Description 方法描述 根据医院id删除医院信息
      *@Date 2022-09-21 15:49:19
       * @param id
      *@Return {@link null}
      */
    @RequestMapping("/delHospital")
    public Object deleteHospitalByHId(@RequestParam("hId") String id){
        log.info("删除医院信息");
        int hId = Integer.valueOf(id);
        log.info("前端传入的值经过转换后,改为:{}",hId);
        boolean updateFlag = hospitalService.deleteHospitalByHId(hId);
        if (updateFlag) {
            log.info("删除医院信息成功");
            return "redirect:viewHospital";
        }
        log.info("删除医院信息失败");
        return "redirect:viewHospital";
    }
    /**
      * @description 跳转到医院审核页面
      * @author GUOYN
      * @date 2022-09-24 22:26:17  * @param hId
     * @param model
      * @return {@link String}
      */
    @RequestMapping("tohospitalaudit")
    public String toHospitalAudit(int hId,Model model){
        model.addAttribute("hId",hId);
        return "admin/hospitalaudit";
    }
    /**
     * @description 跳转到医院修改页面
     * @author GUOYN
     * @date 2022-09-24 22:26:17  * @param hId
     * @param model
     * @return {@link String}
     */
    @RequestMapping("tohospitaluqdate")
    public String toHospitalUqdate(int hId,Model model){
        model.addAttribute("hId",hId);
        return "admin/hospitaluqdate";
    }
    /**
      * @description 根据医院id获得医院详细信息
      * @author GUOYN
      * @date 2022-09-24 22:21:33  * @param hId
     * @param resultMap
      * @return {@link Object}
      */
    @RequestMapping("hospitalviewbyid")
    @ResponseBody
    public Object getHospitalViewById(int hId,Map<String,Object> resultMap){
        Map <String,Object> result = hospitalService.getHospitalViewById(hId);
        log.info("{}",result);
        resultMap.putAll(result);
        log.info("{}",JSONArray.toJSONString(resultMap));
        return JSONArray.toJSONString(resultMap);
    }
}
